<?php session_start();
/*
Template Name: Contact Page
*/
?>
<?php
	$realestate_page_template_settings = array();
	$realestate_page_template_settings = maybe_unserialize( get_post_meta($post->ID,'realestate_page_template_settings',true) );

	$fullwidth = isset( $realestate_page_template_settings['realestate_fullwidthpage'] ) ? (bool) $realestate_page_template_settings['realestate_fullwidthpage'] : false;

	$realestate_regenerate_numbers = isset( $realestate_page_template_settings['realestate_regenerate_numbers'] ) ? (bool) $realestate_page_template_settings['realestate_regenerate_numbers'] : false;

	$realestate_error_message = '';
	$realestate_contact_error = false;

	if ( isset($_POST['realestate_contactform_submit']) ) {
		if ( !isset($_POST['realestate_contact_captcha']) || empty($_POST['realestate_contact_captcha']) ) {
			$realestate_error_message .= '<p>' . esc_html__('Make sure you entered the captcha. ', 'RealEstate') . '</p>';
			$realestate_contact_error = true;
		} else if ( $_POST['realestate_contact_captcha'] <> ( $_SESSION['realestate_first_digit'] + $_SESSION['realestate_second_digit'] ) ) {
			$realestate_numbers_string = $realestate_regenerate_numbers ? esc_html__('Numbers regenerated.', 'RealEstate') : '';
			$realestate_error_message .= '<p>' . esc_html__('You entered the wrong number in captcha. ', 'RealEstate') . $realestate_numbers_string . '</p>';

			if ($realestate_regenerate_numbers) {
				unset( $_SESSION['realestate_first_digit'] );
				unset( $_SESSION['realestate_second_digit'] );
			}

			$realestate_contact_error = true;
		} else if ( empty($_POST['realestate_contact_name']) || empty($_POST['realestate_contact_email']) || empty($_POST['realestate_contact_subject']) || empty($_POST['realestate_contact_message']) ){
			$realestate_error_message .= '<p>' . esc_html__('Make sure you fill all fields. ', 'RealEstate') . '</p>';
			$realestate_contact_error = true;
		}

		if ( !is_email( $_POST['realestate_contact_email'] ) ) {
			$realestate_error_message .= '<p>' . esc_html__('Invalid Email. ', 'RealEstate') . '</p>';
			$realestate_contact_error = true;
		}
	} else {
		$realestate_contact_error = true;
		if ( isset($_SESSION['realestate_first_digit'] ) ) unset( $_SESSION['realestate_first_digit'] );
		if ( isset($_SESSION['realestate_second_digit'] ) ) unset( $_SESSION['realestate_second_digit'] );
	}

	if ( !isset($_SESSION['realestate_first_digit'] ) ) $_SESSION['realestate_first_digit'] = $realestate_first_digit = rand(1, 15);
	else $realestate_first_digit = $_SESSION['realestate_first_digit'];

	if ( !isset($_SESSION['realestate_second_digit'] ) ) $_SESSION['realestate_second_digit'] = $realestate_second_digit = rand(1, 15);
	else $realestate_second_digit = $_SESSION['realestate_second_digit'];

	if ( !$realestate_contact_error ) {
		$realestate_email_to = ( isset($realestate_page_template_settings['realestate_email_to']) && !empty($realestate_page_template_settings['realestate_email_to']) ) ? $realestate_page_template_settings['realestate_email_to'] : get_site_option('admin_email');

		$realestate_site_name = is_multisite() ? $current_site->site_name : get_bloginfo('name');
		wp_mail($realestate_email_to, sprintf( '[%s] ' . esc_html($_POST['realestate_contact_subject']), $realestate_site_name ), esc_html($_POST['realestate_contact_message']),'From: "'. esc_html($_POST['realestate_contact_name']) .'" <' . esc_html($_POST['realestate_contact_email']) . '>');

		$realestate_error_message = '<p>' . esc_html__('Thanks for contacting us', 'RealEstate') . '</p>';
	}
?>
<?php get_header(); ?>

	<div id="content-top">
		<div id="menu-bg"></div>
		<div id="top-index-overlay"></div>

		<div id="content" class="clearfix<?php if($fullwidth) echo(' fullwidth');?>">
			<div id="main-area">
				<?php get_template_part('includes/breadcrumbs'); ?>
				<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
				<div class="full_entry clearfix">
					<?php if (get_option('realestate_integration_single_top') <> '' && get_option('realestate_integrate_singletop_enable') == 'on') echo(get_option('realestate_integration_single_top')); ?>
					<?php $width = 159;
							$height = 159;
							$classtext = '';
							$titletext = get_the_title();
							$thumbnail = get_thumbnail($width,$height,$classtext,$titletext,$titletext);
							$thumb = $thumbnail["thumb"]; ?>
					<div class="entry_content<?php if ($thumb <> '' && get_option('realestate_thumbnails_index') == 'on') echo(' setwidth') ?>">
						<?php if($thumb <> '' && get_option('realestate_page_thumbnails') == 'on') { ?>
							<div class="small-thumb">
								<?php print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext , $width, $height, $classtext); ?>
								<span class="overlay"></span>
							</div><!-- end .small-thumb -->
						<?php }; ?>
						<h1 class="single-title"><?php the_title(); ?></h1>
						<?php the_content(); ?>
						<?php wp_link_pages(array('before' => '<p><strong>'.esc_html__('Pages', 'RealEstate').':</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
						<div id="realestate-contact">
							<div id="realestate-contact-message"><?php echo($realestate_error_message); ?> </div>
							<?php if ( $realestate_contact_error ) { ?>
								<form action="<?php echo(get_permalink($post->ID)); ?>" method="post" id="realestate_contact_form">
									<div id="realestate_contact_left">
										<p class="clearfix">
											<label for="realestate_contact_name" class="realestate_contact_form_label"><?php esc_html_e('Name', 'RealEstate'); ?></label>
											<input type="text" name="realestate_contact_name" value="<?php if ( isset($_POST['realestate_contact_name']) ) echo esc_attr($_POST['realestate_contact_name']); else esc_attr_e('Name', 'RealEstate'); ?>" id="realestate_contact_name" class="input" />
										</p>
										<p class="clearfix">
											<label for="realestate_contact_email" class="realestate_contact_form_label"><?php esc_html_e('Email Address', 'RealEstate'); ?></label>
											<input type="text" name="realestate_contact_email" value="<?php if ( isset($_POST['realestate_contact_email']) ) echo esc_attr($_POST['realestate_contact_email']); else esc_attr_e('Email Address', 'RealEstate'); ?>" id="realestate_contact_email" class="input" />
										</p>
										<p class="clearfix">
											<label for="realestate_contact_subject" class="realestate_contact_form_label"><?php esc_html_e('Subject', 'RealEstate'); ?></label>
											<input type="text" name="realestate_contact_subject" value="<?php if ( isset($_POST['realestate_contact_subject']) ) echo esc_attr($_POST['realestate_contact_subject']); else esc_attr_e('Subject', 'RealEstate'); ?>" id="realestate_contact_subject" class="input" />
										</p>
									</div> <!-- #realestate_contact_left -->
									<div id="realestate_contact_right">
										<p class="clearfix">
											<?php
												esc_html_e('Captcha: ', 'RealEstate');
												echo '<br/>';
												echo esc_attr($realestate_first_digit) . ' + ' . esc_attr($realestate_second_digit) . ' = ';
											?>
											<input type="text" name="realestate_contact_captcha" value="<?php if ( isset($_POST['realestate_contact_captcha']) ) echo esc_attr($_POST['realestate_contact_captcha']); ?>" id="realestate_contact_captcha" class="input" size="2" />
										</p>
									</div> <!-- #realestate_contact_right -->
									<div class="clear"></div>
									<p class="clearfix">
										<label for="realestate_contact_message" class="realestate_contact_form_label"><?php esc_html_e('Message', 'RealEstate'); ?></label>
										<textarea class="input" id="realestate_contact_message" name="realestate_contact_message"><?php if ( isset($_POST['realestate_contact_message']) ) echo esc_textarea($_POST['realestate_contact_message']); else echo esc_textarea( __('Message', 'RealEstate') ); ?></textarea>
									</p>
									<input type="hidden" name="realestate_contactform_submit" value="realestate_contact_proccess" />
									<input type="reset" id="realestate_contact_reset" value="<?php esc_attr_e('Reset', 'RealEstate'); ?>" />
									<input class="realestate_contact_submit" type="submit" value="<?php esc_attr_e('Submit', 'RealEstate'); ?>" id="realestate_contact_submit" />
								</form>
							<?php } ?>
						</div><!-- end #realestate-contact -->
						<div class="clear"></div>
						<?php edit_post_link(esc_html__('Edit this page', 'RealEstate')); ?>
					</div><!-- end .entry_content -->
				</div><!-- .full_entry -->
				<?php if (get_option('realestate_show_pagescomments') == 'on') comments_template('', true); ?>
				<?php endwhile; endif; ?>
			</div><!-- end #main-area -->
			<?php if (!$fullwidth) get_sidebar(); ?>
<?php get_footer(); ?>